package com.lai.hospital.config;

import com.lai.hospital.interception.JwtHandlerInterception;
import com.lai.hospital.interception.PageableHandlerInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author : lai
 * @version V1.0
 * @Project: hospital-springboot
 * @Package com.lai.hospital.config
 * @Description: webmvc配置类
 * @date Date : 2022年11月16日 18:46
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private JwtHandlerInterception jwtHandlerInterception;

    @Autowired
    private PageableHandlerInterceptor pageableHandlerInterceptor;

    /**
     * 处理跨域请求
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**") //对那些请求进行跨域处理
                .allowCredentials(true)//允许的凭证
                .allowedHeaders("*")
                .allowedOriginPatterns("*")
                .allowedMethods("*");
    }

    /**
     * 注册拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(pageableHandlerInterceptor);
//        registry.addInterceptor(jwtHandlerInterception);
    }

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }



}
